package unit;

import hospital.Hospital;

/**
 * A class to create the specific unit Ambulance
 */
public class Ambulance extends Unit {

	/**
	 * Variable registering the hospital where this unit has to go to
	 */
	private Hospital toHospital;

	/**
	 * Initialize this new ambulance with a given name,speed and
	 * garage(homelocation)
	 * 
	 * @param name
	 *            The name for this ambulance
	 * 
	 * @param location
	 *            The location for this ambulance
	 * 
	 * @param garage
	 *            The garage where this ambulance has to be added to
	 * 
	 * @post The name of this new ambulance is the same as the given name.
	 * 
	 * @post The location of this new ambulance is the same as the given
	 *       location.
	 * 
	 * @post This ambulance is added to the given garage
	 * */
	public Ambulance(String name, int speed, Garage garage)
			throws IllegalArgumentException {
		super(name, speed, garage);
	}

	/**
	 * Assign the hospital where to this ambulance has to drive his victims
	 * 
	 * @param h
	 *            the hospital where to drive the victims to
	 * 
	 * @post the toHospital of this ambulance is set
	 * 
	 * @throws IllegalArgumentException
	 *             this ambulance does not have an emergency assigned this
	 *             ambulance has not yet reached the location of the emergency
	 */
	public void assignHospital(Hospital h) {
		if (this.isAssigned()
				&& this.getCurrentLocation().equals(
						this.getEmergency().getLocation())) {
			this.setToHospital(h);
		} else {
			String error = "";
			if (!isAssigned())
				error += " niet geassigned";

			throw new IllegalArgumentException(
					"Invalid operation for this ambulance! " + error);
		}
	}

	/**
	 * Method for breaking down ambulances => can't!
	 * 
	 * @throws IllegalArgumentException
	 *             , an ambulance can't break down
	 */
	@Override
	public void brokeDown() {
		throw new IllegalArgumentException("An ambulance can't break down");
	}

	/**
	 * Method for checking this ambulance can finish it's emergency ( if it's
	 * has an emergency assigned )
	 * 
	 * @return true if this ambulance can finish it's emergency, false otherwise
	 */
	@Override
	public boolean emergencyCanBeFinished() {
		try {
			if (getEmergency() != null
					&& getCurrentLocation().equals(
							getToHospital().getLocation()))
				return true;
			else
				return false;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * Getter returning the hospital the ambulance has to drop its victims
	 * 
	 * @return the hospital of this ambulance
	 */
	public Hospital getToHospital() {
		return toHospital;
	}

	/**
	 * Method for setting the Hospital where this ambulance has to go to
	 * 
	 * @param h
	 *            The hospital where this ambulance has to go to
	 * 
	 * @post the toHospital of this ambulance is set to the given hospital
	 * 
	 * @post the target location of this ambulance is set to the hospital's
	 *       location
	 */
	private void setToHospital(Hospital h) {
		this.toHospital = h;
		this.setTargetLocation(h.getLocation());
	}

	/**
	 * Method for getting the total description of this Ambulance
	 * 
	 * @return Returns the total description of this Ambulance
	 */
	@Override
	public String showInfo() {
		String output = "Ambulance " + this.getName() + "\n";
		if (this.isAssigned()) {
			output += "current assignment: " + this.getEmergency().toString()
					+ "\n";
			if (this.getCurrentLocation().equals(
					this.getEmergency().getLocation())) {
				output += "unit is at emergency\n";
				if (this.getToHospital() != null) {
					output += "selected hospital = "
							+ this.getToHospital().getName() + "\n";
				}
			} else {
				output += "unit isn't at emergency\n";
			}
		} else
			output += "unit is waiting for an emergency\n";
		return output;
	}

	/**
	 * Return the type of unit, in this case ambulance
	 * 
	 * @return The type of this unit, in this case ambulance
	 */
	@Override
	public String toString() {
		return "Ambulance-" + getName();
	}

	/**
	 * Method for checking this ambulance can start its task
	 * 
	 * @return Returns true, ambulances don't have to wait for their "collegues"
	 */
	@Override
	public boolean unitsOfSameTypeOnEmergency() {
		if (getEmergency() != null) {
			// ambulances don't have to wait for their collegues
			return true;
		} else {
			throw new IllegalArgumentException(
					"This ambulance doesn't has an emergency");
		}

	}
}
